var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    max: [],
    min: [],
    aniamtion: '',
    miaoshu: "",
    state: 0,
    _max: 0,
    _min: 0,
    display: true,
    page:1,
    commodityList:[],
    imgurl: app.globalData.url
  },
  // Math.random();  //0.0 ~ 1.0 之间的一个伪随机数。【包含0不包含1】 //比如0.8647578968666494

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
  },
  /**
   * 检验授权
   */
  loginCheck:function(){

    var memberInfo = wx.getStorageSync('memberInfo');
    var userInfo = wx.getStorageSync('userInfo');
    if(memberInfo ==null|| userInfo==null ||memberInfo=='' || userInfo==''){//未授权登录
      wx.navigateTo({
        url: '../login/login'
      })

    }
  },



  // 获取最小值到最大值之前的整数随机数
  random: function (Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
  },
  // 方法
  randoms: function () {
    let that = this
    // 最小值
    let minarr = this.data.min
    let arr1 = minarr.push(that.random(0, 9))
    // 最大值
    let maxarr = this.data.max
    let arr2 = maxarr.push(that.random(0, 9))

    that.setData({ min: minarr, max: maxarr })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear'
    })
  },
  translate: function () {
    let that = this
    var num = 1
    if (this.data.display == false) {
      return
    }
    this.setData({
      display: false
    })
    var aaa = setInterval(function () {
      let minarr = that.data.min
      let maxarr = that.data.max
      let minarr1 = minarr.push(that.random(0, 10))
      let maxarr1 = maxarr.push(that.random(0, 10))
      that.animation.translate(0, -(115 * (num - 1))).step()
      that.setData({ animation: that.animation.export(), min: minarr, max: maxarr })
      num += 1;
    }, 1000)
    setTimeout(function () {
      clearInterval(aaa)
      that.miao(),
        that.setData({
          display: true
        })
    }, 4000)
  },
  // 点击秒数
  miao: function () {
    let that = this
    let miao = 10
    let arr = this.data.max
    console.log("max" + arr)
    let arr1 = this.data.min
    console.log("min" + arr1)
    var bbb = setInterval(function () {
      miao -= 1
      if (miao <= 0) {
        clearInterval(bbb)
      }
      that.setData({ miaoshu: miao, _max: arr[arr.length - 2], _min: arr1[arr1.length - 2] })
    }, 1000)

    this.setData({
      state: 1
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.randoms();
    this.loginCheck();
    this.getCommodityList();
  },
  // 重置
  clear:function(){
    this.setData({
      min:[],
      max:[],
      state:0
    })
    this.randoms();
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    //this.randoms();
    this.loginCheck();
    this.getCommodityList();
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    this.loginCheck();
    this.getCommodityList();
    setTimeout(function(){
        wx.hideLoading();

    },1000)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //去商品详情页
  list:function(e){
    var id = this.data.commodityList[e.currentTarget.id].id
    console.log(id);

    wx.navigateTo({
      url: '../list_detail/list_detail?id='+id,
    })
  },
 /**
  * 获取商品列表
  */
  getCommodityList:function(page){
    var that = this;
    wx.request({
      url: app.globalData.url + "/applet/commodityList",
      data: {
        page: 1,
        // parentId: wx.getStorageSync('parentId')
      },
      success: function (res) {

        console.log(res.data);
        if(res.data.code=='000000'){
          that.setData({
            commodityList:res.data.data
          });
        }
      }
    })

  }

})